<?php
namespace Modules\User\Entities;

use Modules\Core\Entities\BaseEntity as Model;
use Modules\Order\Traits\Skuable;


use ThrowException;

class VipSku extends Model
{
	use Skuable;
    protected $guarded = [];
    protected $table = 'vip_skus';

    protected $casts = [
    ];

    static public $resourceName = 'VIP套餐';

    public function getNodeEntity()
    {
        return $this;
    }

    public function getType($type = 'node_type') 
    {
        $map = [
            'base_sku_type' => 'vip_sku',
            'node_type' => 'vip_sku'
        ];

        return @$map[$type];
    }

    public function sendTo($user)
    {
    	$calcStart = date('Y-m-d 00:00:00');

    	// 1为开通 2为续费
    	$purchaseType = 1;

    	// vip正常状态（续费）
    	if ($user->vip_status === 1) {
    		$calcStart = (String)$user->vip_end_at;
	    	$purchaseType = 2;
    	}

    	$calcEnd = date('Y-m-d 23:59:59', strtotime($calcStart) + 86400 * $this->days);

    	$user->update([
    		'vip_status' => 1,
    		'vip_start_at' => $this->vip_start_at ?: $calcStart,
    		'vip_end_at' => $calcEnd
    	]);

    	return [
    		$calcStart,
    		$calcEnd,
    		$purchaseType
    	];
    }

    // 返回商品类型
    public function getProductType()
    {
        return 'vip';
    }
}
